<template>
  <div class="table-page-search-wrapper">
    <a-card>
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="12">
            <a-form-item
                label="学号"
                :labelCol="{span: 7,offset: 1}"
                :wrapperCol="{span: 15, offset: 1}"
            >
              <a-input v-model="queryParam.xh"/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="12">
            <a-form-item
              label="姓名"
              :labelCol="{span: 7,offset: 1}"
              :wrapperCol="{span: 15, offset: 1}"
            >
              <a-input v-model="queryParam.xm"/>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="8" :sm="12">
              <a-form-item
                label="学生类别"
                :labelCol="{span: 7,offset: 1}"
                :wrapperCol="{span: 15, offset: 1}"
              >
                <a-select v-model="queryParam.xslb">
                  <a-select-option v-for="i in TYPE0075" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="12">
              <a-form-item
                label="学生来源"
              >
                <a-select :allowClear="true" v-model="queryParam.xslxm">
                  <a-select-option v-for="i in TYPE0503" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="12">
              <a-form-item label="院系">
                <a-select allowClear v-model="queryParam.yx" :disabled="selectMenu.yxList.length === 1" @change="yxChange" showSearch :filterOption="filterOption">
                  <a-select-option v-for="i in selectMenu.yxList" :key="i.id" :value="i.dwh">{{ i.dwh }} {{ i.dwmc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="12">
              <a-form-item label="专业">
                <a-select allowClear v-model="queryParam.zy" showSearch :filterOption="filterOption">
                  <a-select-option v-for="i in selectMenu.zyList" :key="i.id" :value="i.zyh">{{ i.zyh}} {{ i.zymc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="12">
              <a-form-item label="年级">
                <a-select allowClear v-model="queryParam.nj">
                  <a-select-option v-for="i in TYPE0126" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="12" :sm="12">
            <span class="table-page-search-submitButtons" >
              <a-button type="primary" @click="$refs.table.refresh(true)" style="margin:0 10px">查询</a-button>
              <a-button type="primary" @click="showBatchCheck" style="margin:0 10px">批量审核</a-button>
              <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </a-card>

    <a-card :bordered="false" style="margin-top: 10px">
      <s-table
        v-if="init"
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns"
        :data="loadData"
        :alert="options.alert"
        :rowSelection="options.rowSelection"
        :pageSize="parameter.pageSize"
        :scroll="{x: 1500}"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 + (parameter.pageNo - 1) * parameter.pageSize }}
        </span>
        <template slot="id" slot-scope="id">
          <a @click="showCheck(id)">审核</a>
        </template>
      </s-table>
    </a-card>

    <audit-modal
      ref="check"
      :visible="visible"
      :checkId="checkId"
      @cancel="visible = false"
      @ok="checkPaper"
    />
  </div>
</template>

<script>
import { STable } from '~'
import { mapState } from 'vuex'
import auditModal from './auditModal'

export default {
  data () {
    return {
      advanced: true,
      queryParam: {},
      columns: [
        {
          title: '序号',
          scopedSlots: { customRender: 'serial' }
        },
        {
          title: '学号',
          dataIndex: 'xh'
        },
        {
          title: '姓名',
          dataIndex: 'xm'
        },
        {
          title: '评阅结果',
          dataIndex: 'lwpyjgmc'
        },
        {
          title: '院系',
          dataIndex: 'yxmc'
        },
        {
          title: '专业',
          dataIndex: 'zymc'
        },
        {
          title: '导师',
          dataIndex: 'dsxm'
        },
        {
          title: '年级',
          dataIndex: 'ssnjmc'
        },
        {
          title: '学生类别',
          dataIndex: 'xslbmc'
        },
        {
          title: '论文题目',
          dataIndex: 'lwtm'
        },
        {
          title: '操作',
          dataIndex: 'id',
          scopedSlots: { customRender: 'id' },
          fixed:'right',
          width:80
        }
      ],
      parameter: {
        pageNo: 1,
        pageSize: 10
      },
      selectedRowKeys: [],
      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      loadData: parameter => {
        this.parameter = parameter
        parameter.pageNum = parameter.pageNo
        let params = Object.assign(this.queryParam, parameter)
        return this.$api.degree.paperCheckList(params).then(res => {
          return {
            data: res.data.rows,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total
          }
        })
      },
      selectMenu: {
        yxList: []
      },
      visible: false,
      init: false,
      checkId: ''
    }
  },
  components: {STable, auditModal},
  computed: {
    ...mapState({
      TYPE0075: state => state.app['TYPE0075'],
      TYPE0503: state => state.app['TYPE0503'],
      TYPE0126: state => state.app['TYPE0126']
    })
  },
  created () {
    this.$store.dispatch('app/setDictionary', 'TYPE0075')
    this.$store.dispatch('app/setDictionary', 'TYPE0126')
    this.$store.dispatch('app/setDictionary', 'TYPE0503')
    this.initSelectMenu()
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll(),
        this.$store.dispatch('app/sysInfo')
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yxList: res[0].data
        }
        if (res[0].data.length === 1) {
          this.$set(this.queryParam, 'yx', res[0].data[0].dwh)
          this.yxChange(res[0].data[0].dwh)
        }
        this.$set(this.queryParam, 'nj', res[1].currentGrade)
        this.init = true
      })
    },
    filterOption (value, op) {
      return op.componentOptions.children[0].text.indexOf(value) !== -1
    },
    yxChange (code) {
      this.$set(this.queryParam, 'zyh', '')
      if (code) {
        this.$api.base.majorAll({lsdwh: code}).then(res => {
          this.$set(this.selectMenu, 'zyList', res.data)
        })
      } else {
        this.$set(this.selectMenu, 'zyList', [])
      }
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
    },
    showBatchCheck() {
      if (!this.selectedRowKeys.length) {
        return this.$message.warn('请选择要审核的论文')
      }
      this.checkId = this.selectedRowKeys.join(',')
      this.visible = true
      this.$nextTick(() => {
        this.$refs.check.form.resetFields()
      })
    },
    showCheck (id) {
      this.visible = true
      this.checkId = id
      this.$nextTick(() => {
        this.$refs.check.form.resetFields()
      })
      // this.$api.cultivate.midTermDetail(xh).then(res => {
      //   this.$refs.check.detail = res.data
      // })
    },
    checkPaper(params) {
      this.$api.degree.paperCheck(params).then(res => {
        this.visible = false
        this.$message.success(res.msg)
        this.doRefresh()
      })
    },
    doRefresh(){
      this.$refs.table.refresh(false)
    }
  }
}
</script>

<style>

</style>
